package org.blackcat.web;

import org.blackcat.core.annotation.Login;
import org.blackcat.domain.common.MsgResponse;
import org.blackcat.domain.enums.UserEnum;
import org.blackcat.domain.User;
import org.blackcat.service.UserService;
import org.blackcat.web.filter.LoginedUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by zz   on 2016/12/23.
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping(value="",method = RequestMethod.GET)
    public MsgResponse<User> login(String account, String password) throws Exception {
        return userService.findByAccount(account,password);
    }
    @RequestMapping(value="",method = RequestMethod.PUT)
    public MsgResponse<User> create(User user, HttpServletResponse response) throws Exception {
        return userService.create(user);
    }
    @RequestMapping(value="",method = RequestMethod.POST)
    @Login
    public MsgResponse<Object> update(User user, HttpServletRequest request) throws Exception {
        String token = (String) request.getAttribute(UserEnum.USER_REQUEST_TOKEN.getValue());
        return userService.update(user,token);
    }
    @RequestMapping(value = "/existsAccount",method = RequestMethod.GET)
    public MsgResponse<Object> existsAccount(String account){
        return userService.existsAccount(account);
    }
}
